package com.ruoyi.system.service.impl;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.security.auth.JWTUser;
import com.ruoyi.framework.security.auth.RequestContext;
import com.ruoyi.system.domain.UserSystemMsg;
import com.ruoyi.system.mapper.UserOfficialMsgMapper;
import com.ruoyi.system.mapper.UserSystemMsgMapper;
import com.ruoyi.system.pojo.AppSystemMsgDTO;
import com.ruoyi.system.service.AppSystemPushService;

@Service
public class AppSystemPushServiceImpl implements AppSystemPushService {

	@Autowired
	private UserSystemMsgMapper userSystemMsgMapper;
	@Autowired
	private UserOfficialMsgMapper userOfficialMsgMapper;
	
	/**
	 * 	获取用户ID
	 * @return
	 */
	private Long getLoginUserId() {
		Object object = RequestContext.get("userInfo");
		if(null == object) {
			return null;
		}
		JWTUser jwtUser = (JWTUser) object;
		if(null == jwtUser || null == jwtUser.getId()) {
			return null;
		}
		return jwtUser.getId();
	}
	
	@Override
	public AjaxResult getSystemMsg(Integer pageNo, Integer type, HttpServletRequest request) {
		try {
			Long userId = getLoginUserId();
			if(null == userId) {
				return AjaxResult.error(50010,"请重新登录");
			}
			//pageNo = (pageNo -1)*10;
			LambdaQueryWrapper<UserSystemMsg> lambdaQueryWrapper = new LambdaQueryWrapper<UserSystemMsg>();
			lambdaQueryWrapper.eq(UserSystemMsg::getType, type);
			lambdaQueryWrapper.and(tmp -> tmp.eq(UserSystemMsg::getUserId, userId).or().eq(UserSystemMsg::getUserId, 0));
			lambdaQueryWrapper.orderByDesc(UserSystemMsg::getCreateTime);
			Page<UserSystemMsg> systemMsg = userSystemMsgMapper.selectPage(new Page<UserSystemMsg>().setCurrent(pageNo).setSize(10), lambdaQueryWrapper);
			return AjaxResult.success(systemMsg.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("网络繁忙");
		}
	}
	
	@Override
	public AjaxResult getLatestMsg() {
		try {
			Long userId = getLoginUserId();
			if(null == userId) {
				return AjaxResult.error(50010,"请重新登录");
			}
			LambdaQueryWrapper<UserSystemMsg> lambdaQueryWrapper = new LambdaQueryWrapper<UserSystemMsg>();
			lambdaQueryWrapper.eq(UserSystemMsg::getType, 1);
			lambdaQueryWrapper.and(tmp -> tmp.eq(UserSystemMsg::getUserId, userId).or().eq(UserSystemMsg::getUserId, 0));
			lambdaQueryWrapper.orderByDesc(UserSystemMsg::getCreateTime);
			lambdaQueryWrapper.last("limit 1");
			UserSystemMsg systemMsg = userSystemMsgMapper.selectOne(lambdaQueryWrapper);
			return AjaxResult.success(systemMsg);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("网络繁忙");
		}
	}

	@Override
	public AjaxResult getOfficialMsg(Integer pageNo, HttpServletRequest request) {
		try {
			Long userId = getLoginUserId();
			if(null == userId) {
				return AjaxResult.error(50010,"请重新登录");
			}
			//pageNo = (pageNo -1)*10;
			IPage<AppSystemMsgDTO> officialMsg = userOfficialMsgMapper.getOfficialMsg(new Page<AppSystemMsgDTO>().setCurrent(pageNo).setSize(10), userId);
			List<AppSystemMsgDTO> list = officialMsg.getRecords();
			if(CollectionUtils.isNotEmpty(list)) {
				list.parallelStream().forEach(msg ->{
					if(2 == msg.getType()) {
						List<String> asList = Arrays.asList(msg.getImg().split(","));
						asList.forEach(System.out::println);
						msg.setImgList(asList);
						msg.setImg("");
					}
				});
			}
			return AjaxResult.success(list);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("网络繁忙");
		}
	}
}
